package cn.bigfire.wedesk.util;

import cn.bigfire.wedesk.bean.Wedesk;
import cn.bigfire.wedesk.store.Stores;
import cn.hutool.core.swing.ScreenUtil;

import java.awt.image.BufferedImage;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ScreenTimer {
    //调用一下为了初始化和开启定时任务
    public static void init(){

    }

    public static BufferedImage before;//上一个截图

    public static  ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(10);

    static {
        executor.scheduleAtFixedRate(ScreenTimer::captureAndSend, 0, 300, TimeUnit.MILLISECONDS);//截图频率
    }

    //截屏方法
    public static void captureAndSend() {
        try {
            //有控制者，才截图,发送数据
            if (Stores.hasCtrl()) {
//                long timeStart = System.currentTimeMillis();
                BufferedImage curr = ScreenUtil.captureScreen();
//                System.out.println("截图时间：" + (System.currentTimeMillis() - timeStart));
                long timeMillis = System.currentTimeMillis();
                // 不管控制者需要的尺寸，只关注自身大小，图片尺寸不一致 只能是本机调整分辨率了
                if (before != null && (before.getWidth() != curr.getWidth() || before.getHeight() != curr.getHeight())) {
                    System.out.println("图片尺寸不一致");
                    before = null;
                }
                // 1. 求差
                int width = curr.getWidth();
                int height = curr.getHeight();
                int[] currRGB = curr.getRGB(0, 0, width, height, null, 0, width);
                int[] bitmap = new int[currRGB.length];
                int changedColors = 0, start = 0, end = bitmap.length;
                if (before != null) {
                    int[] beforeRGB = before.getRGB(0, 0, width, height, null, 0, width);
                    for (int i = 0; i < bitmap.length; i++) {
                        if (beforeRGB[i] == currRGB[i]) {
                            bitmap[i] = 0;
                        } else {
                            if (start == 0) start = i;
                            else end = i;
                            changedColors += 1;
                            bitmap[i] = currRGB[i];
                        }
                    }
                } else {
                    bitmap = currRGB;
                }
                if (before != null && changedColors == 0) {
                    return;//无像素变动
                }

                ;
                // Log.debug("Changed colors: " + changedColors);
                // 2. 压缩
//                System.out.println("求差时间：" + (System.currentTimeMillis() - timeStart));
                byte[] compressedData = new RLEncoding().compress(bitmap, start, end);
//                System.out.println("压缩时间：" + (System.currentTimeMillis() - timeStart));
                //        byte[] bytes = ImgUtil.img2Byte(curr);
                //        System.out.println("from: " + start + ", to: " + end);
                //        System.out.println("颜色总数:" + bitmap.length + ",变动数:" + changedColors + ",占比:" + changedColors * 100 / bitmap.length);
                //        System.out.println("原来数据:" + bytes.length + ",压缩数据:" + compressedData.length + ",压缩比:" + compressedData.length * 100 / bytes.length);
                before = curr;
                Wedesk.ScreensHot screensHot = new Wedesk.ScreensHot();
                screensHot.setWidth(curr.getWidth());
                screensHot.setHeight(curr.getHeight());
                screensHot.setData(compressedData);
                Stores.blockingQueue.offer(ScreenData.of(timeMillis, screensHot),1000, TimeUnit.MILLISECONDS);//尝试往里面放，如果超过一秒就放弃
//                PackUtil.sendImgData(screensHot);
//                System.out.println("--------------------------------------------------------------------------------------");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
